package cutetd.action;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.opensymphony.xwork2.ActionSupport;

import cutetd.dao.GoodsDao;
import cutetd.dao.OrderDao;
import cutetd.model.M_Goods;
import cutetd.model.M_Order;
import cutetd.vo.Page;
import cutetd.vo.PageNavi;

public class OrderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<M_Order> orderList = new ArrayList<M_Order>();
	private M_Order order;
	private OrderDao orderDao = new OrderDao();
	private String keyString;
	GoodsDao goodsDao = new GoodsDao();
	private List<M_Goods> goodsList;

	private String orderState;
	private String orderId;
	
	private List<String> goodsIdList;
	private List<String> goodsNumList;
	
	HashMap<String,String> orderHash = new HashMap<String, String>();
	
	// 控制分页属性
	private int curPage = 1;
	private List<Page> pageList = new ArrayList<Page>();
	private int totalPage;
	private PageNavi pageNavi = new PageNavi(M_Order.class, 2, 5);

	@SuppressWarnings("unchecked")
	public String manager() {
		pageList = pageNavi.getPageList(curPage);
		orderList = (List<M_Order>) pageNavi.getObjectList(curPage,
				"postDate != '1'", "postDate asc");
		totalPage = pageNavi.getTotalPages();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String historyOrder() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String owner = (String) session.getAttribute("email");
		pageList = pageNavi.getPageListByCondition(curPage, "sender=='" + owner
				+ "'");
		orderList = (List<M_Order>) pageNavi.getObjectList(curPage, "sender=='"
				+ owner + "'", "postDate asc");
		totalPage = pageNavi
				.getTotalPagesByCondition("sender=='" + owner + "'");
		return "his";
	}

	public String addOrder() {
		String keyString = UUID.randomUUID().toString();
		
		if (goodsIdList != null) {
			order.setKeyString(keyString);
			order.setPostDate(new Date());
			order.setState("waiting");
			
			Set<Key> keys = new HashSet<Key>();			
			
			for (int i = 0; i < goodsIdList.size(); ++i) {
				keys.add(KeyFactory.createKey("M_Goods", goodsIdList.get(i)));
			}
			
			order.setGoodsList(keys);
			order.setGoodsNumList(goodsNumList);
			orderDao.saveOrUpdate(order);
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			session.setAttribute("message", "订单添加成功");
		}
		
		return "manager";
			/*for (int i = 0; i < goodsIdList.size(); ++i) {
				// M_Goods goods =
				// goodsDao.getObjectByKey(goodsList.get(i).getId());
				keys.add(KeyFactory.createKey("M_Goods", goodsIdList.get(i)));
				
				order.setGoodsList(keys);
				orderDao.saveOrUpdate(order);
			}
			
			setMessage("订单添加成功");
		}*/
		/*if (goodsList != null) {
			order.setKeyString(keyString);
			order.setPostDate(new Date());
			order.setState("waiting");
			// List<M_Goods> list = new ArrayList<M_Goods>();
			Set<Key> keys = new HashSet<Key>();

			for (int i = 0; i < goodsList.size(); ++i) {
				// M_Goods goods =
				// goodsDao.getObjectByKey(goodsList.get(i).getId());
				keys.add(KeyFactory.createKey("M_Goods", goodsList.get(i)
						.getId()));
			}
			order.setGoodsList(keys);
			orderDao.saveOrUpdate(order);
		}
		// System.out.println(order.getKeyString());
		// orderDao.saveOrUpdate(order);

		// 测试用。之后删除
		GoodsDao goodsDao = new GoodsDao();
		goodsList = (List<M_Goods>) goodsDao.getListAll();*/
		
	}

	public String toAdd() {
		// 测试用。之后删除
		GoodsDao goodsDao = new GoodsDao();
		goodsList = (List<M_Goods>) goodsDao.getListAll();

		return "toAdd";
	}

	public String toUp() {

		order = orderDao.getObjectByKey(keyString);

		Set<Key> keys = order.getGoodsList();
		java.util.Iterator<Key> it = keys.iterator();
		List<String> list = new ArrayList<String>();

		while (it.hasNext()) {
			Key key = it.next();
			list.add(key.getName());
			// System.out.println(key.getName());
		}
		setOrder(order);
		
		
		goodsList = goodsDao.getObjectByKetList(list);
		goodsNumList = order.getGoodsNumList();
		
		Iterator<M_Goods> glist = goodsList.listIterator();
		Iterator<String> nlist = goodsNumList.listIterator();
		
		while (glist.hasNext() && nlist.hasNext()) {
			//System.out.println
			orderHash.put(glist.next().getDisplayName(), nlist.next());
		}
		//goodsList = goodsDao.getObjectByKetList(list);
		//goodsNumList = order.getGoodsNumList();

		String state = order.getState();
		if (state.equals("waiting") || state.equals("sending")) {
			return "toUp";
		} else {
			return "toHisUp";
		}
	}

	public String updateOrder() {
		if (orderState != null) {
			order = orderDao.getObjectByKey(orderId);

			if (order != null) {
				order.setState(orderState);
				HttpServletRequest request = ServletActionContext.getRequest();
				HttpSession session = request.getSession();
				String type = (String) session.getAttribute("type");
				if (type != null && !type.equals("waiting")) {
					order.setSender((String) session.getAttribute("email"));
				}
				orderDao.saveOrUpdate(order);
			}
		}
		return "manager";
	}

	public String deleteOrder() {
		// System.out.println(keyString);
		orderDao.deleteByKey(keyString);
		return "manager";
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public List<Page> getPageList() {
		return pageList;
	}

	public void setPageList(List<Page> pageList) {
		this.pageList = pageList;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public PageNavi getPageNavi() {
		return pageNavi;
	}

	public void setPageNavi(PageNavi pageNavi) {
		this.pageNavi = pageNavi;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public List<M_Order> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<M_Order> orderList) {
		this.orderList = orderList;
	}

	public M_Order getOrder() {
		return order;
	}

	public void setOrder(M_Order order) {
		this.order = order;
	}

	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public List<M_Goods> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<M_Goods> goodsList) {
		this.goodsList = goodsList;
	}

	public String getKeyString() {
		return keyString;
	}

	public void setKeyString(String keyString) {
		this.keyString = keyString;
	}

	public String getOrderState() {
		return orderState;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderState(String orderState) {
		this.orderState = orderState;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public List<String> getGoodsIdList() {
		return goodsIdList;
	}

	public void setGoodsIdList(List<String> goodsIdList) {
		this.goodsIdList = goodsIdList;
	}

	public List<String> getGoodsNumList() {
		return goodsNumList;
	}

	public void setGoodsNumList(List<String> goodsNumList) {
		this.goodsNumList = goodsNumList;
	}

	public HashMap<String, String> getOrderHash() {
		return orderHash;
	}

	public void setOrderHash(HashMap<String, String> orderHash) {
		this.orderHash = orderHash;
	}
}
